package com.linyu.huawei.simple;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 斗地主
 * 输入：
 *   3-3-3-3-4-4-5-5-6-7-8-9-10-J-Q-K-A
 *   4-5-6-7-8-8-8
 * 输出:
 *   9-10-J-Q-K-A
 */
public class FightDiZhu {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入（第一行：手上已有的牌；第二行：已经出过的牌（自己和对手））：");
        String firstLineStr = sc.next();
        String secondLineStr = sc.next();
        List<String> firstList = new ArrayList<>(Arrays.asList(firstLineStr.split("-")));
        List<String> secondList = new ArrayList<>(Arrays.asList(secondLineStr.split("-")));
        for (int i = 0; i < firstList.size(); i++) {
            String str = firstList.get(i);
                for (int j = 0; j < secondList.size(); j++) {
                    if(secondList.get(j).equals(str)){
                        secondList.remove(j);
                        firstList.remove(i);
                        break;
                    }
                }
        }
        // 我 firstList 剩下的就是可能用来拼的字符
        for (int i = 0; i < firstList.size(); i++) {
            // 将所有的 J Q K A 转换为 11，12，13，14
            if (firstList.get(i).equals("J")){
                firstList.set(i, "11");
            }else if (firstList.get(i).equals("Q")){
                firstList.set(i, "12");
            }else if (firstList.get(i).equals("K")){
                firstList.set(i, "13");
            }else if (firstList.get(i).equals("A")){
                firstList.set(i, "14");
            }
            if (firstList.get(i).equals("2") || firstList.get(i).equals("大王") || firstList.get(i).equals("小王")){
                firstList.remove(i);
            }
        }
        // 先全转换为 int 类型 便于比较
        List<Integer> lastList = firstList.stream().map(one -> Integer.parseInt(one)).collect(Collectors.toList());
        // 用指针去做
        String resultStr = "" + lastList.get(0); // 准备拼接字符
        ArrayList<String> resultList = new ArrayList<>(); // 准备收集结果集
        for (int i = 1; i < lastList.size(); i++) {
            if (lastList.get(i) - lastList.get(i - 1) == 1){
                //如果可以承接，直接更改 b 的下标
                resultStr = resultStr + "-" + lastList.get(i);
                if (i == lastList.size() -1){
                    resultList.add(resultStr);
                }
                continue;
            }else if (lastList.get(i) - lastList.get(i - 1) == 0){
                if (i == lastList.size() -1){
                    resultList.add(resultStr);
                }
                continue;
            }
            // 如果不符合直接断开,并且去重置 str
            if (resultStr.split("-").length >= 5){
                resultList.add(resultStr);
            }
            resultStr = "" + lastList.get(i);
        }
//        System.out.println(resultList);
        // 应为保不齐，字符长度导致的元素少，长度长
        String[] strings = resultList.stream().map(one -> one.split("-")).max((a, b) -> a.length - b.length).get();
        for (int i = 0; i < strings.length; i++) {
            // 转回去
            if (strings[i].equals("11")){
                strings[i] = "J";
            }else if (strings[i].equals("12")){
                strings[i] = "Q";
            }else if (strings[i].equals("13")){
                strings[i] = "K";
            }else if (strings[i].equals("14")){
                strings[i] = "A";
            }
        }
        String result = String.join("-", strings);
        System.out.println(result);
    }

}
